mod peer;

pub use peer::*;

use std::sync::RwLock;

lazy_static::lazy_static! {

    pub(crate) static ref PEER_ID: RwLock<Option<String>> = RwLock::new(None);

    pub(crate) static ref RRAI_TOKEN: RwLock<Option<String>> = RwLock::new(None);
}

pub fn set_token(token: &String) -> anyhow::Result<()> {
    //
    let mut token_option = RRAI_TOKEN
        .write()
        .map_err(|err| anyhow::anyhow!("{}", err))?;

    *token_option = Some(token.clone());
    Ok(())
}

pub fn get_token() -> anyhow::Result<String> {
    RRAI_TOKEN
        .read()
        .map_err(|err| anyhow::anyhow!("{}", err))?
        .as_ref()
        .map_or(Err(anyhow::anyhow!("没有设置Token")), |token| {
            Ok(token.clone())
        })
}

pub fn set_peer_id(peer_id: &String) -> anyhow::Result<()> {
    //
    let mut token_option = PEER_ID.write().map_err(|err| anyhow::anyhow!("{}", err))?;

    *token_option = Some(peer_id.clone());
    Ok(())
}

pub fn get_peer_id() -> anyhow::Result<String> {
    PEER_ID
        .read()
        .map_err(|err| anyhow::anyhow!("{}", err))?
        .as_ref()
        .map_or(Err(anyhow::anyhow!("没有设置peer_id")), |token| {
            Ok(token.clone())
        })
}
